CheckPopLogin(function () {
    /* 变量 */
    var personal = "user/personal.html";
    var userInfo = "user/userInfo.html";

    /* 个人中心  */
    var contactsCallback_personal = myApp.onPageInit('personal', function (page) {

        var _userID = iss_localStorage.get("yuqing_appUserId"); // 全局变量

        //加载基本信息
        loadInfo();
        function loadInfo() {
            var getUser = JSON.stringify({
                type: 13,
                user: { "ID": _userID }
            });
            $$.ajax({
                url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.USERINFO,
                data: { content: getUser },
                dataType: "jsonp",
                method: "POST",
                success: function (data, status) { 
                    try {
                        data = JSON.parse(data);
                        data = data.results;
                        //城市
                        if ($$("#personal_page p.name").length > 0) {
                            $$("#personal_page p.address")[0].innerHTML = "<span></span>" + data.cityName;
                            //姓名
                            $$("#personal_page p.name").html(data.nickname);
                        }
                        iss_localStorage.set("yuqing_appUserNickName", data.nickname);
                        iss_localStorage.set("yuqing_appUserId", data.ID);
                        iss_localStorage.set("yuqing_appUserName", data.name);
                        iss_localStorage.set("yuqing_appPhone", data.phoneNumber);
                        iss_localStorage.set("yuqing_appIdCard", data.cardNo);
                        //积分
                        var scoreCurrent = "";
                        if (parseFloat(data.scoreCurrent) == "0") {
                            scoreCurrent = "0";
                        } else {
                            scoreCurrent = data.scoreCurrent;
                        }
                        scoreCurrent = scoreCurrent + " 分";
                        //根据字段长度判断span长度
                        var spanWidth = scoreCurrent.length * 13 + "px";
                        var spanMarginLeft = "-" + (scoreCurrent.length * 13 - 30) + "px";
                        if ($$("#scorecurrent").length > 0) {
                            $$("#scorecurrent").html(scoreCurrent);
                            $$("#scorecurrent").css("width", spanWidth);
                            $$("#scorecurrent").css("margin-left", spanMarginLeft);
                        }
                        //加载头像，如果手机号没有就只用默认头像
                        // if(data.phone && data.phone != ""){
                        //loadUserImg(data.phone);
                        //}else{
                        //loadUserImg(data.phoneNumber);
                        //}                    
                        if (data.userAvatar && data.userAvatar != "") {
                            //如果不是nopicture就更新
                            if (data.userAvatar.indexOf("System.Net.WebException") <= -1
                                && data.userAvatar.indexOf("noPicture.jpg") <= -1) {
                                if ($$("#personal_page .personal_infro img").length > 0) {
                                    $$("#personal_page .personal_infro img").attr("src", data.userAvatar);
                                }
                                iss_localStorage.set("yuqing_appUserSRC", data.userAvatar)
                            }
                        }
                    } catch (ex) {
                        myApp.alert("获取个人信息失败！");
                    }
                },
                error: function (xhr, status) {
                    myApp.alert("获取个人信息失败！");
                    myApp.hidePreloader();
                    console.log("ajax error!");

                }
            });
        }

        //加载用户头像
        function loadUserImg(phone) {
            var conditionParams = {
                type: 2,
                serviceName: ISS_Constants.PortalServiceName.GetUserAvatar,
                content: JSON.stringify({
                    format: "json",
                    phone: phone,
                }),
            };
            $$.ajax({
                url: ISS_Constants.Url.const_portal_url,
                data: conditionParams,
                dataType: "json",
                crossDomain: true,
                success: function (data, status) {
                    try {
                        //如果不是nopicture就更新
                        if (data.indexOf("System.Net.WebException") <= -1
                            && data.indexOf("noPicture.jpg") <= -1) {
                            $$("#personal_page .personal_infro img").attr("src", data);
                            iss_localStorage.set("yuqing_appUserSRC", data)
                        }
                    } catch (ex) {
                        myApp.alert("获取个人信息失败！");
                    }
                },
                error: function (xhr, status) {
                    myApp.alert("获取个人信息失败！");
                    myApp.hidePreloader();
                    console.log("ajax error!");

                }
            });
        }

        $$("#loginout").click(function () {
            // iss_localStorage.set("yuqing_appOpenId", "");
            iss_localStorage.set("yuqing_appUserId", "");
            iss_localStorage.set("yuqing_appPhone", "");
            iss_localStorage.set("yuqing_appNickName", "");
            iss_localStorage.set("yuqing_isLoginOut", "true");
            window.location.href = _home;
        });

        
    });

    /* 个人资料  */
    var contactsCallback_userinfo = myApp.onPageInit('userinfo', function (page) {
        var _userID = iss_localStorage.get("yuqing_appUserId"); // 全局变量

        //加载资料
        loadInfo();
        function loadInfo() {
            var getUser = JSON.stringify({
                type: 13,
                user: { "ID": _userID }

            });

            $$.ajax({
                url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.USERINFO,
                data: { content: getUser },
                dataType: "jsonp",
                method: "POST",
                success: function (data, status) {
                    data = JSON.parse(data);
                    if (data && data.status == "0") {
                        var sex = "";
                        sex = data.results.sex == true ? "1" : "0";
                        data.results.sex = data.results.sex == true ? "男" : "女";
                        var searchTemplate = $$('#template-resturant-detail').html();
                        var compiledSearchTemplate = Template7.compile(searchTemplate);
                        var html = compiledSearchTemplate(data.results);
                        $$("#userinfo_page .page-content-resturant").html(html);
                        $$("#update_nickname").val(data.results.nickname);
                        $$("#calendarBasic-birthday").val(data.results.birthday);
                        $$("#update_username").val(data.results.name);
                        $$("#update_email").val(data.results.email);
                        $$("#update_cityname").val(data.results.cityName);
                        $$("input[name='my-radio'][value='" + sex + "']").prop("checked", true);
                        $$(".userinfo .calendar").on("click", function () {
                            firstMonth = generateMonthEvents(now.getFullYear(), now.getMonth());
                            calendarBasicInit();
                            $('#calendarBasic-birthday').mobiscroll('show');
                        })
                    }
                }
            });
        }

        /*时间选择器*/
        var booked = [];
        var now = new Date();
        var firstMonth;

        function calendarBasicInit() {
            $(function () {
                booked = firstMonth.events;
                // Mobiscroll Calendar initialization
                $('#calendarBasic-birthday').mobiscroll().calendar({
                    theme: 'mobiscroll',
                    lang: 'zh',
                    display: 'bottom',
                    events: booked,
                    onSet: function (event, inst) {
                        // myApp.alert(event.valueText);
                       $$('#calendarBasic-birthday').val(ISS.utils.dateToStr("yyyy-MM-dd", new Date(event.valueText)));
                    }
                });

                $('#calendarBasic-show').click(function () {
                    $('#calendarBasic-birthday').mobiscroll('show');
                    return false;
                });

                $('#calendarBasic-clear').click(function () {
                    $('#calendarBasic-birthday').mobiscroll('clear');
                    return false;
                });

            });
        }

        function generateMonthEvents(year, month) {
            var events = [];
            
            return {
                events: events
            };
        }

        $$("#userinfo_page #btn-save-nickname").click(function () {
            var nickname = $$('#update_nickname').val();
            if (nickname != "") {
                var userContent = JSON.stringify({
                    type: 12,
                    user: { "ID": _userID, "Nickname": nickname }

                });

                // 初始化数据
                $$.ajax({
                    url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.USERINFO,
                    data: { content: userContent },
                    dataType: "jsonp",
                    method: "POST",
                    success: function (data, status) {
                        data = JSON.parse(data);
                        loadInfo();
                        myApp.closeModal(".popup-nickname");
                    },
                    error: function (xhr, status) {
                        console.log("ajax error!");
                    }
                });
            }
        });

        $$("#userinfo_page #btn-save-sex").click(function () {
            var sex = $$('#update_sex input[type="radio"]:checked').val();
            if (sex && (sex == "0" || sex == "1")) {
                var isTrue = sex == "1" ? "true" : "false";
                var uContent = JSON.stringify({
                    type: 12,
                    user: { "ID": _userID, "Sex": isTrue }

                });

                // 初始化数据
                $$.ajax({
                    url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.USERINFO,
                    data: { content: uContent },
                    dataType: "jsonp",
                    method: "POST",
                    success: function (data, status) {
                        data = JSON.parse(data);
                        loadInfo();

                        myApp.closeModal(".popup-sex");
                    },
                    error: function (xhr, status) {
                        console.log("ajax error!");
                    }
                });
            }
        });

        $$("#userinfo_page #btn-save-birthday").click(function () {
            var birthday = $$('#calendarBasic-birthday').val();
            if (birthday != "") {
                var userContent = JSON.stringify({
                    type: 12,
                    user: { "ID": _userID, "Birthday": birthday }

                });

                // 初始化数据
                $$.ajax({
                    url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.USERINFO,
                    data: { content: userContent },
                    dataType: "jsonp",
                    method: "POST",
                    success: function (data, status) {
                        data = JSON.parse(data);
                        loadInfo();
                        myApp.closeModal(".popup-birthday");
                    },
                    error: function (xhr, status) {
                        console.log("ajax error!");
                    }
                });
            }
        });

        $$("#userinfo_page .list-block .telephone .item-inner").click(function () {
            myApp.alert("手机号码不可更改");
        })

        $$("#userinfo_page #btn-save-username").click(function () {
            var username = $$('#update_username').val();
            if (username != "") {
                var userContent = JSON.stringify({
                    type: 12,
                    user: { "ID": _userID, "Name": username }

                });

                // 初始化数据
                $$.ajax({
                    url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.USERINFO,
                    data: { content: userContent },
                    dataType: "jsonp",
                    method: "POST",
                    success: function (data, status) {
                        data = JSON.parse(data);
                        loadInfo();
                        myApp.closeModal(".popup-username");
                    },
                    error: function (xhr, status) {
                        console.log("ajax error!");
                    }
                });
            }
        });

        $$("#userinfo_page #btn-save-email").click(function () {
            var email = $$('#update_email').val();
            if (email != "") {
                var userContent = JSON.stringify({
                    type: 12,
                    user: { "ID": _userID, "Email": email }

                });

                // 初始化数据
                $$.ajax({
                    url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.USERINFO,
                    data: { content: userContent },
                    dataType: "jsonp",
                    method: "POST",
                    success: function (data, status) {
                        data = JSON.parse(data);
                        loadInfo();
                        myApp.closeModal(".popup-email");
                    },
                    error: function (xhr, status) {
                        console.log("ajax error!");
                    }
                });
            }
        });

        $$("#userinfo_page #btn-save-cityname").click(function () {
            var cityname = $$('#update_cityname').val();
            if (cityname != "") {
                var userContent = JSON.stringify({
                    type: 12,
                    user: { "ID": _userID, "CityName": cityname }

                });

                // 初始化数据
                $$.ajax({
                    url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.USERINFO,
                    data: { content: userContent },
                    dataType: "jsonp",
                    method: "POST",
                    success: function (data, status) {
                        data = JSON.parse(data);
                        loadInfo();
                        myApp.closeModal(".popup-cityname");
                    },
                    error: function (xhr, status) {
                        console.log("ajax error!");
                    }
                });
            }
        });
        
    });

    function initData() {
        var href = window.location.href;
        if (href.indexOf(personal) > -1) {
            contactsCallback_personal.trigger();
        }
        else if (href.indexOf(userInfo) > -1) {
            contactsCallback_userinfo.trigger();
        }

    }

    initData();
});